import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);
export default new Vuex.Store({
  state: {
    count: 0,
    message: 'hello',
    userCount: 10,
    todos: [
      {id: 1, text: '学习Vue', done: true},
      {id: 2, text: '学习Vuex', done: false}
    ],
    dictionary: {
      sex: {
        '1': '男',
        '2': '女'
      }
    }
  },
  getters: {
    todoDoneCount: state => {
      return state.todos.filter(item => item.done).length;
    }
  },
  // mutation method 不能直接调用
  mutations: {
    incrementCount(state, payload) {
      state.count += payload.count;
    },
    printLog() {
      console.log('hello -> mutations')
    }
  },
  actions: {
    incrementCount(context, payload) {
      setTimeout(() => {
        context.commit('incrementCount', payload)
      }, 1000);
    },
    // 异步处理
    actionA({commit}) {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          commit('printLog');
          resolve();
        }, 500)
      })
    }
  }
})
